<?php

class Wallmodel extends Model
{
		//Constructor
		function Wallmodel()
		{
				parent::Model();

		}
		
		function wallPost($userId, $wallId, $wallContent, $wallType)
		{
			$SQL = 'INSERT INTO wall (wall_for_id, user_id, wall_content, wall_date, wall_type)
			VALUES(	
				' . $this->db->escape($wallId) . ',
				' . $this->db->escape($userId) . ',
				' . $this->db->escape($wallContent) . ',
				' . 'NOW()' . ',
				'.  $this->db->escape($wallType) .'
				)';
				
				$this->db->query($SQL);
		}
		
		function subWallPost($userId, $refWallId, $subWallContent)
		{
			$SQL = 'INSERT INTO sub_wall (ref_wall_id, user_id, sub_wall_content, sub_wall_date)
			VALUES(	
				' . $this->db->escape($refWallId) . ',
				' . $this->db->escape($userId) . ',
				' . $this->db->escape($subWallContent) . ',
				' . 'NOW()' . '
				)';
				
				$this->db->query($SQL);
		}
		/// test
		function get_wall($wallForId, $orderBy = 'wall_date desc', $start = '', $limit = '')
		{
				$this->db->select('wall_id, wall_for_id, wall.user_id, username, avatar, wall_content, wall_date');
				$this->db->from('wall');
				$this->db->join('users', 'wall.user_id = users.user_id');
				$this->db->where('wall_for_id', $wallForId);
				if (trim($orderBy) != '') $this->db->order_by($orderBy);
				if (trim($limit) != '' && is_numeric($limit))
				{
						if (trim($start) != '' && is_numeric($start)) $this->db->limit($limit, $start);
						else  $this->db->limit($limit);
				}
				$wallQuery = $this->db->get();
				$wall = array();
				if ($wallQuery->num_rows() > 0)
				{
						foreach ($wallQuery->result_array() as $wallRow)
						{
								$this->load->model('usermodel');
								//$wallRow['avatar'] = $this->usermodel->getAvatar($wallRow['user_id']);
								$wall[$wallRow['wall_id']] = $wallRow;
						}
				}
				return json_encode($wall);
				
		}
		/// end
		function getWall($wallForId, $orderBy = 'wall_date desc', $start = '', $limit = '')
		{
				$this->db->select('wall_id, wall_for_id, wall.user_id, username, avatar, wall_content, wall_date, wall_type');
				$this->db->from('wall');
				$this->db->join('users', 'wall.user_id = users.user_id');
				$this->db->where('wall_for_id', $wallForId);
				if (trim($orderBy) != '') $this->db->order_by($orderBy);
				if (trim($limit) != '' && is_numeric($limit))
				{
						if (trim($start) != '' && is_numeric($start)) $this->db->limit($limit, $start);
						else  $this->db->limit($limit);
				}
				$wallQuery = $this->db->get();
				$wall = array();
				if ($wallQuery->num_rows() > 0)
				{
						foreach ($wallQuery->result_array() as $wallRow)
						{
								$this->load->model('usermodel');
								//$wallRow['avatar'] = $this->usermodel->getAvatar($wallRow['user_id']);
								$wall[$wallRow['wall_id']] = $wallRow;
						}
				}
				return $wall;
		}
		
		function getSubWall($refWallId, $orderBy = 'sub_wall_date asc', $start = '', $limit = '')
		{
				$this->db->select('sub_wall_id, sub_wall.user_id, username, avatar, sub_wall_content, sub_wall_date');
				$this->db->from('sub_wall');
				$this->db->join('users', 'sub_wall.user_id = users.user_id');
				$this->db->where('ref_wall_id', $refWallId);
				if (trim($orderBy) != '') $this->db->order_by($orderBy);
				if (trim($limit) != '' && is_numeric($limit))
				{
						if (trim($start) != '' && is_numeric($start)) $this->db->limit($limit, $start);
						else  $this->db->limit($limit);
				}
				$wallQuery = $this->db->get();
				$subWall = array();
				if ($wallQuery->num_rows() > 0)
				{
						foreach ($wallQuery->result_array() as $wallRow)
						{
								$this->load->model('usermodel');
								$wallRow['avatar'] = $this->usermodel->getAvatar($wallRow['user_id']);
								$subWall[$wallRow['sub_wall_id']] = $wallRow;
						}			
				}	
				return $subWall;			
		}
		
		function wallUpdate($wallForId)
		{
			$this->db->select('wall_content');
			$this->db->from('wall');
			$this->db->where('wall_for_id', $wallForId);
			$this->db->where('user_id', $wallForId);
			$this->db->order_by('wall_date', 'desc');
			$this->db->limit(1);
			$wallUpdateQuery = $this->db->get();
			if($wallUpdateQuery->num_rows() > 0)
			{
				$wallUpdateRow = $wallUpdateQuery->result_array();
				$wallUpdate = $wallUpdateRow[0]['wall_content'];
				return $wallUpdate;
			}
			//else return $wallForId;
		}
		
		function getAllWall()
		{
			$orderBy = 'wall_date desc';
			
			$this->db->select('*');
			$this->db->order_by($orderBy);
			$query = $this->db->get('wall');
			if($query->num_rows() > 0)
			{
				foreach($query->result_array() as $row)
					{
						$wall[] = $row;
					}
				
				return $wall;
			}
			else 
			{
			return FALSE;
			}
		}
		
		function deleteWall($wallId)
		{
				$this->db->delete('wall', array('wall_id' => $wallId));
		}
		
		function deleteSubWall($subWallId)
		{
				$this->db->delete('sub_wall', array('sub_wall_id' => $subWallId));
		}
		
		function ajax_more($wallForId, $orderBy = 'wall_date desc', $start = '', $limit = '', $lastmsg = '')
		{
				$this->db->select('wall_id, wall_for_id, wall.user_id, username, avatar, wall_content, wall_date, wall_type');
				$this->db->from('wall');
				$this->db->join('users', 'wall.user_id = users.user_id');
				$this->db->where('wall_for_id', $wallForId);
				$this->db->where('wall_id <', $lastmsg);
				if (trim($orderBy) != '') $this->db->order_by($orderBy);
				if (trim($limit) != '' && is_numeric($limit))
				{
						if (trim($start) != '' && is_numeric($start)) $this->db->limit($limit, $start);
						else  $this->db->limit($limit);
				}
				$wallQuery = $this->db->get();
				$wall = array();
				if ($wallQuery->num_rows() > 0)
				{
						foreach ($wallQuery->result_array() as $wallRow)
						{
								$this->load->model('usermodel');
								//$wallRow['avatar'] = $this->usermodel->getAvatar($wallRow['user_id']);
								$wall[$wallRow['wall_id']] = $wallRow;
						}
				}
				return $wall;
		}
}
